package top.wuyang.springboot3.schoolTimetable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.wuyang.springboot3.base.PageBean;
import top.wuyang.springboot3.base.R;
import top.wuyang.springboot3.schoolTimetable.dao.TbTeacherInfoMapper;
import top.wuyang.springboot3.schoolTimetable.entity.TbTeacherInfo;
import top.wuyang.springboot3.schoolTimetable.service.TbTeacherInfoService;
import top.wuyang.springboot3.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbTeacherInfoServiceImpl implements TbTeacherInfoService {
    private final TbTeacherInfoMapper tbTeacherInfoMapper;

    @Override
    public R<PageVO<TbTeacherInfo>> query(TbTeacherInfo tbTeacherInfo, PageBean pageBean){
        QueryWrapper<TbTeacherInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbTeacherInfo.getTname()), "tname", tbTeacherInfo.getTname());
        IPage<TbTeacherInfo> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        page = tbTeacherInfoMapper.selectPage(page, wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbTeacherInfo> queryById(Integer id){
        TbTeacherInfo tbTeacherInfo = tbTeacherInfoMapper.selectById(id);
        return tbTeacherInfo == null ? R.fail("数据不存在", null) : R.ok(tbTeacherInfo);
    }

//    private TbTeacherInfo queryByTname(String tname){
//        QueryWrapper<TbTeacherInfo> wrapper = new QueryWrapper<>();
//        wrapper.eq("tname", tname);
//        return tbTeacherInfoMapper.selectOne(wrapper);
//    }

    @Override
    public R<String> insert(TbTeacherInfo tbTeacherInfo){
        int i = tbTeacherInfoMapper.insert(tbTeacherInfo);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }

    @Override
    public R<String> update(TbTeacherInfo tbTeacherInfo){
        int i = tbTeacherInfoMapper.updateById(tbTeacherInfo);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

    @Override
    public R<String> delete(Integer id){
        int i = tbTeacherInfoMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }




}
